package sctu.edu.java.year2016.team08.project;

/**
 * Created by 松红 on 2016/6/29.
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.xml.crypto.Data;
import java.util.Date;

public class Scanf extends JFrame {
    public Scanf() {
        Date date = new Date();                 //定义时间对象
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        Container c = getContentPane();
        setLayout(new GridLayout(4, 1, 10, 10));        //面板以及布局
        JTextArea area = new JTextArea(String.format("现在时间是：" + "%tT", date) + "    你使用该系统，请输入问题\n");
        JTextField field = new JTextField();
        JButton button = new JButton("已有问题搜索");        //搜索按钮
        JButton button1 = new JButton("后台客服登陆");        //登陆按钮
        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                c.add(area);
            } else if (i == 1) {
                c.add(field);
            } else if (i == 2) {
                c.add(button);
            } else if (i == 3) {
                c.add(button1);
            }

        }                               //四个组件的布局
        setVisible(true);
        setTitle("IT运维问题输入系统");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        field.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                area.append(field.getText() + '\n');
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e3) {
                    e3.printStackTrace();
                }
                try {
                    String url = "jdbc:mysql://localhost:3306/lsongh";
                    String user = "root";
                    String Password = "";
                    Connection conn = DriverManager.getConnection(url, user, Password);
                    String string = null;
                    string = field.getText();
                    field.setText(" ");
                    String sql1 = "insert into itrunQ (question)values('" + string + "')";
                    PreparedStatement ps1 = conn.prepareStatement(sql1);
                    ps1.execute(sql1);
                    conn.close();
                } catch (SQLException e4) {
                    e4.printStackTrace();
                }
            }
        });
        // TODO Auto-generated method stub
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Search search1 = new Search();
                search1.setVisible(true);
            }
        });
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Client client = new Client();
                client.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Scanf();
    }

}